u`
Ep.212uCfNTv̕⑫p.213uindex01.csv

p.212 CfNTiČfځj

EɃf[^ołz񓙂vpeBƓlɈd|CfNT
EvpeBƂ͈قȂACfNTɂ͖OȂuCX^X̎Qƕϐ[Y]v̌`ŁAz̗vf
E`F public ^ this[CfbNX^ CfbNX] { get {c; return l⎮;} set {c;} }
@ ^͈z񓙂ɍ킹
@ get̒ɂĔz񓙂̗vf[CfbNX]̒lԂ
@ set̒ł͊O^lvalueL[[hœ邱ƂłAʏAvf[CfbNX]ɑ
E{IȒ`F public ^ this[int i] { get {return z[i];} set {z[i] = value;} }
E̍ӂɎQƕϐ[Y]w肷ƁAset̓esBFQƕϐ[0] = l;
E̍ӈȊOɎQƕϐ[Y]w肷ƁAget̓esBFConsole.Write(Qƕϐ[0]);

p.213 indexer01.cs

//p.213 indexer01.cs
using System;
class MyClass {
    string[] name = new string[5]; //CfNTňz
    public string this[int i] { //CfNT̒`(CfbNX͐i)
        get { return name[i]; }
        set { name[i] = value; }
    }
}
class indexer01 {
    public static void Main() {
        MyClass mc = new MyClass();
        mc[0] = "Y"; //CfNToRname[0]ɑ
        mc[1] = "Y"; //CfNToRname[1]ɑ
        mc[2] = "OY"; //CfNToRname[2]ɑ
        mc[3] = "lY"; //CfNToRname[3]ɑ
        mc[4] = "ܘY"; //CfNToRname[4]ɑ
        for (int i = 0; i < 5; i++) {
            Console.WriteLine(mc[i]); //CfNToRname[0]`[4]𓾂ĕ\
        }
    }
}

p.214iCfNTň\𓮓Iɐꍇj

ECfNTňf[^\(Fz)̎̂̓NXɌŒ蒷ŔzuKv͂ȂB
EQƂ݂̂`ĂāARXgN^ŗvf̕ΗǂB
EAvfێd|KvB

p.214 indexer02.cs

//p.214 indexer02.cs
using System;
class MyIndexer {
    int[] array; //CfNTňz̒`()
    int nMax; //vf
    public int this[int n] { //CfNT̒`(CfbNX͐i)
        get {
            if (n < nMax) { //vf`FbN
                return array[n];
            } else {
                return 0;
            }
        }
        set {
            if (n < nMax) { //vf`FbN
                array[n] = value;
            }
        }
    }
    public MyIndexer(int i) { //RXgN^(vf)
        array = new int[i]; //vf̗̕vf𐶐
        nMax = i; //vfZbg
    }
}
class indexer02 {
    public static void Main() {
        MyIndexer mi = new MyIndexer(20); //CX^X𐶐RXgN^oRŗvfn
        for (int i = 0; i < 20; i++) { //SvfɂČJԂ
            mi[i] = i * i;
        }
        for (int i = 0; i < 20; i++) { //SvfɂČJԂ
            Console.WriteLine("{0} * {0} = {1}", i, mi[i]);
        }
        //킴Ƃɔz͈̔͂𒴂
        mi[30] = 30; //zȂΎsG[ACfNTset30n邾
        Console.WriteLine("mi[30] = {0}", mi[30]);
    }
}

p.217 CfbNXƂCfNT

ECfNT̃CfbNX͐lłKv͂Ȃ
ECfNT̒`́upublic int this[int n] {c}ṽCfbNXłň^intłȂĂǂ

p.217 indexer03.cs

//p.217 indexer03.cs
using System;
class MyIndexer {
    string[] mon = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec"}; //CfNTňz
    public int this[string MonthName] { //CfNX^łCfNT(get̂)
        get {
            for (int i = 0; i < 12; i++) { //z̑SvfɂČJԂ
                if (MonthName == mon[i]) { //CfbNXƈv
                    return i + 1; //Y+1ČɂĕԂ
                }
            }
            return 0; //Ă͂܂̂Ȃ0Ԃ
        }
    }
}
class indexer03 {
    public static void Main() {
        MyIndexer mi = new MyIndexer();
        Console.WriteLine("May{0}Ԗڂ̌ł", mi["May"]); //5
        Console.WriteLine("Dec{0}Ԗڂ̌ł", mi["Dec"]); //12
        Console.WriteLine("x{0}Ԗڂ̌ł",   mi["x"]);   //0
    }
}

p.218 ̃CfNT

ECfNX𕡐̃CfNT`ł
EzƓlɗpł

p.219 indexer04.cs

//p.219 indexer04.cs
using System;
class MyClass {
    string[,] name; //CfNTňQz̒`()
    public string this[int i, int j] { //QCfNTiset͖j
        get {
            return name[i, j]; //Qz̗vfԂ
        }
    }
    public MyClass() { //RXgN^
        name = new string[,]{{"c", "", "gc", "", "H"},
                             {"H", "|", "ē", "c", "{"}}; //2~5̓񎟌z𐶐
    }
}
class indexer04 {
    public static void Main() {
        MyClass mc = new MyClass();
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                Console.WriteLine("{0}g{1}--{2}", i + 1, j + 1, mc[i, j]); //Q̃CfNTŌĂ
            }
        }
    }
}

p.220 CfNT̃I[o[[h

ECfNT͖ÔŁACfbNX̐ƌ^VOj`ƂȂ
EāACfbNX̐ƌ^قȂCfNT`ł
ECfNT̃I[o[[hƂ
E\bh̏ꍇƓlɁA߂l^̓VOj`Ɋ܂܂Ȃ̂ŒӁB

p.220 indexer05.cs

//p.220 indexer05.cs
using System;
class MyOverLoad {
    int[] a = new int[3] { 1, 2, 3 }; //CfNT@ňPz
    int[,] b = new int[2, 2] { { 100, 200 }, { 300, 400 } }; //CfNTAňQz
    public int this[int i] { //CfNT@
        get { return a[i]; } //Pz̗vf[CfbNX]Ԃ
    }
    public int this[int i, int j] { //CfNTA
        get { return b[i, j]; } //Qz̗vf[CfbNXi,CfbNXj]Ԃ
    }
}
class indexer05 {
    public static void Main() {
        MyOverLoad mo = new MyOverLoad();
        for (int i = 0; i < 3; i++) { //Pz̗lq̕AJԂ
            Console.WriteLine("mo[{0}] = {1}", i, mo[i]);
        }
        for (int i = 0; i < 2; i++) { //Qz̊O̗lq̕AJԂ
            for (int j = 0; j < 2; j++) { //Qz̗̓lq̕AJԂ
                Console.WriteLine("mo[{0}, {1}] = {2}", i, j, mo[i, j]);
            }
        }
    }
}

AWKFp.220 indexer05.cs

ECfbNX̃CfNTƕ̃CfNTł邱ƂmF悤

쐬

//AWKFp.220 indexer05.cs
using System;
class MyOverLoad {
    string[] a = new string[3] { "amuro", "shar", "ryu" }; //CfNT@AňPz
    public string this[int i] { //int^̃CfNT@
        get { return a[i]; } //z̗vf[CfbNX]Ԃ
    }
    public int this[string s] { //string^̃CfNTA
        get { //z̗vf[Y]ƈvYԂ
            for (int i = 0; i < a.Length; i++) {
                if(a[i] == s) return i; 
            }
            return -1; //v̂Ȃ-1Ԃ
        } 
    }
}
class indexer05 {
    public static void Main() {
        MyOverLoad mo = new MyOverLoad();
        Console.WriteLine("mo[1] = {0}", mo[1]); //int^̃CfNT@p
        Console.WriteLine("mo[\"shar\"] = {0}", mo["shar"]); //string^̃CfNTAp
    }
}

p.222 KP qg

Ep.209 prop02.csPƗǂ
Edouble^̃f[^oP̂ݎNXƂAQڂ̃vpeBBMIvZ͊
Edouble^̃f[^oɓKȏl^ĂAvpeBŕ̐0^āAlωȂƂmF悤

쐬

//p.222 KP
using System;
class BMI {
    double bl; //J̃CX^XϐFg
    public double blprop { //gp̃vpeB
        get { return bl; }
        set {
            if (value <= 0) { //l0ȉH
                return; //IĖ߂
            }
            bl = value; //gɑl
        }
    }
}
class prop02 {
    public static void Main() {
        BMI mybmi = new BMI();
        mybmi.blprop = 175.2; //vpeBoRsets
        Console.WriteLine("bl = {0}" ,mybmi.blprop); //vpeBŐg𓾂ĕ\
        mybmi.blprop = -90.3; //vpeBoRsets(̐Ȃ̂Őݒ肳Ȃ)
        Console.WriteLine("bl = {0}" ,mybmi.blprop); //vpeBŐg𓾂ĕ\
    }
}

p.222 KQ qg

E蕶ɎĂȂdl͎RɌ߂ėǂBL͈B
Ekz̗vfRXgN^(int)Ŏ󂯎
Estring^̐k̔z
Evfint^̓_̔z𐶐
ECfNT[string]`
@Egetł͐k̔z𒲂ׂāA΂̓YpāA_̔z[Y]̒lԂ
@EȂ(knullɂȂ)A-1Ԃ
@Esetł͐k̔z𒲂ׂāA΂̓YpāA_̔z[Y]̒lvalueɕύX
@EȂ(knullɂȂ)A̓YpāAk̔z[Y]ɖOA_̔z[Y]ɓ_i[
@ ȂĐknullɂȂĂȂΖȂ̂ŁA̎|\

쐬

//p.222 KQ
using System;
class Exam {
    string[] name; //CfNTňO̔z
    int[] point; //CfNTň_̔z
    int num = 0; //k
    public int this[string s] { //OCfNXƂē_CfNT
        get { 
            for (int i = 0; i < num; i++) { //SkɂČJԂ
                if(name[i] == s) { //OvH
                    return point[i]; //̓_Ԃ
                } else if (name[i] == null) { //H
                    break; //JԂI
                }
            }
            return -1; //OvȂ-1Ԃ
        }
        set {
            for (int i = 0; i < num; i++) { //SkɂČJԂ
                if (name[i] == null) { //v󂫂LH
                    name[i] = s; //Oi[
                    point[i] = value; //̓_i[
                    return; //CfNT𔲂
                } else if(name[i] == s) { //OvH
                    point[i] = value; //̓_XV
                    return; //CfNT𔲂
                }
            }
            Console.WriteLine("I");
        }
    }
    public Exam(int num) { //k󂯎RXgN^
        this.num = num;
        name = new string[num]; //O̔z𐶐
        point = new int[num]; //_̔z𐶐
    }
    public void show() {
        for (int i = 0; i < num; i++) {
            if (name[i] != null) { //łȂ
                Console.Write("{0}:{1}, ", name[i], point[i]);
            }
        }
        Console.WriteLine(); //s
    }
}
class indexer05 {
    public static void Main() {
        Exam e = new Exam(4);
        e["amuro"] = 60;
        e.show(); //uamuro:60,v
        e["shar"] = 70;
        e.show(); //uamuro:60, shar:70,v 
        e["amuro"] = 80;
        e.show(); //uamuro:80, shar:70,v
        e["bright"] = 90;
        e.show(); //uamuro:80, shar:70, bright:90,v
        e["ryu"] = 50;
        e.show(); //uamuro:80, shar:70, bright:90, ryu:50,v
        e["kai"] = 40; //uIv
        e.show(); //uamuro:60, shar:70, bright:90, ryu:50,v
    }
}

X p

p.223 NX̌p̊b

ENX͕iɗLAƊǗȂ(Ƀ`[Jł)Ӗ߂NXAẽNXɋLqĖN₷B
Ȇ΍ƂāANXɐeq֌WAeqɏ񂪈p悤ɂ̂p
EeɂNX{NXAqɂNXhNXƂ
EāAhNX͊{NXp
EhNX̒`Ɋ{NXw肷邾ŁA{NXɂ郁o(ق)ׂČp
EA{NXƁApeς̂ŁACf
EāAhNXł͓Ǝ̃ô݋LqΗǂ
@F
@@XCNXHPƐ킢`
@AXCNXpzC~XCNXMPƎ`(HPƐ킢͌p̂ŋLqsv)
EhNXph̔hNXLqłA{NXƔhNX̗p
EȂAC#ł͂Qȏ̊{NX(dp)͋֎~
EtɁAQȏ̔hNXƂ͉\
EpCw^XƂ

p.223iNX̌pj

E`F class hNX : {NX {c}
EȂA{NXɂprivatew̃o͌pȂ

p.224 inheritance01.cs

//p.224 inheritance01.cs
using System;
//{NX
class MyBase {
    public int a = 10; //p\ȃf[^o
    public void BaseMethod() { //p\ȃ\bh
        Console.WriteLine("͊{NXł");
    }
}
//hNX
class MyDerived : MyBase { //MyBase{NXƂhNX
    //Ɂupublic int a = 10;vp
    //Ɂupublic void BaseMethod() {c}vp
    public int b = 20; //ǉ̃f[^o
    public void DerivedMethod() { //ǉ̃\bh
        Console.WriteLine("͔hNXł");
    }
}
class inheritance01 {
    public static void Main() {
        MyDerived md = new MyDerived(); //hNX̃CX^X𐶐
        md.BaseMethod(); //p\bhhNX̃CX^Xɂ
        md.DerivedMethod(); //ǉ̃\bhhNX̃CX^Xɂ
        Console.WriteLine("md.a = {0}", md.a); //pf[^ohNX̃CX^Xɂ
        Console.WriteLine("md.b = {0}", md.b); //ǉ̃f[^ohNX̃CX^Xɂ
        MyBase mb = new MyBase(); //{NX̃CX^Xł
        mb.BaseMethod(); //{NX̃\bh͊{NX̃CX^Xɂ
        Console.WriteLine("mb.a = {0}", mb.a); //{NX̃f[^o{NX̃CX^Xɂ
        //umb.DerivedMethod();vȂG[ɂȂ
    }
}

p.226 protectedɂf[^̕ی

Eprivateȃo͌pȂAp̂߂publicɂ̂͗ǂȂ
EŁAprivateƓlɕی삵Ap͉\Ƃ̂protected
EāAp֎~oprivateɂƗǂ

p.226 inheritance02.cs

//p.226 inheritance02.cs
using System;
class Base { //{NX
    protected int x = 20; //Jp͉\ȃf[^o
}
class Derived : Base { //hNX
    //Ɂuprotected int x = 20;vƂ݂Ȃ
    int y = 10;
    public void ShowXY() {
        // ̃NXBaseNXx͌
        Console.WriteLine("x = {0}, y = {1}", x, y);
    }
}
class inheritance02 {
    public static void Main() {
        Derived md = new Derived(); //hNX̃CX^X𐶐
        md.ShowXY(); //hNX̃\bhs(Ŋ{NX̃f[^o𗘗p)
    }
}

p.228 ỎB؂

EhNXŊ{NXƓÕoLqł
E̎ɑOɁunewvt邱ƂŁAỎB؂ƂȂuOɂ邱ƂŕBvƂɂȂ
 ㏑ł͂Ȃ̂ŁA{NX̃\bhs@񋟂Ă

p.228 inheritance03.cs

//p.228 inheritance03.cs
using System;
class Base { //{NX
    public int x = 10; //p\ȃf[^o
    public void BaseMethod() { //p\ȃ\bh
        Console.WriteLine("BaseNXł");
    }
}
class Derived : Base { //hNX
    new public int x = 20; //f[^o̖ỎB؂
    new public void BaseMethod() { //\bh̖ỎB؂
        Console.WriteLine("DerivedNXł");
    }
}
class inheritance03 {
    public static void Main() {
        Derived d = new Derived(); //hNX̃CX^X𐶐
        Console.WriteLine("x = {0}", d.x); //B؂f[^op
        d.BaseMethod(); //B؂\bhs
    }
}

AWKFp.228 inheritance03.cs

E{NX̗oprotectedɂĂAỎB؂N邱(ʂɂȂ邱)mF悤

oFAWKFp.228 inheritance03.cs

\Fp.229ibaseL[[hj

